<?php

namespace app\admin\controller;

class Product extends Common {
        /*
         * 推荐
         */

        public function tours_show_good() {
                $id = I('get.id');
                $act = I('get.act');
                $data = array();
                $data['uid'] = get_member_id();
                $data['dateline'] = time();
                if ($act == 'do') {
                        $data['isgood'] = '1';
                        $this->sysAdd("执行了【推荐线路】操作，id=" . $id);
                } elseif ($act == 'un') {
                        $data['isgood'] = '0';
                        $this->sysAdd("执行了【取消推荐线路】操作，id=" . $id);
                } else {
                        $this->iError("非法操作！");
                }
                $ret = M("Tours")->where(['id' => ['in', explode(',', $id)]])->data($data)->save();
                if ($ret !== false) {
                        $this->delSuccess('操作成功');
                } else {
                        $this->iError("数据库异常，请联系管理员解决。");
                }
        }

        /*
         * 置顶
         */

        public function tours_show_istop() {
                $id = I('get.id');
                $act = I('get.act');
                $data = array();
                $data['uid'] = get_member_id();
                $data['dateline'] = time();
                if ($act == 'do') {
                        $data['istop'] = '1';
                        $this->sysAdd("执行了【置顶线路】操作，id=" . $id);
                } elseif ($act == 'un') {
                        $data['istop'] = '0';
                        $this->sysAdd("执行了【取消置顶线路】操作，id=" . $id);
                } else {
                        $this->iError("非法操作！");
                }
                $ret = M("Tours")->where(['id' => ['in', explode(',', $id)]])->data($data)->save();
                if ($ret !== false) {
                        $this->delSuccess('操作成功');
                } else {
                        $this->iError("数据库异常，请联系管理员解决。");
                }
        }

        /*
         * 特惠
         */

        public function tours_show_tehui() {
                $id = I('get.id');
                $act = I('get.act');
                $data = array();
                $data['uid'] = get_member_id();
                $data['dateline'] = time();
                if ($act == 'do') {
                        $data['ispreference'] = '1';
                        $this->sysAdd("执行了【特惠线路】操作，id=" . $id);
                } elseif ($act == 'un') {
                        $data['ispreference'] = '0';
                        $this->sysAdd("执行了【取消特惠线路】操作，id=" . $id);
                } else {
                        $this->iError("非法操作！");
                }
                $ret = M("Tours")->where(['id' => ['in', explode(',', $id)]])->data($data)->save();
                if ($ret !== false) {
                        $this->delSuccess('操作成功');
                } else {
                        $this->iError("数据库异常，请联系管理员解决。");
                }
        }

        /*
         * 首推
         */

        public function tours_show_index() {
                $id = I('get.id');
                $act = I('get.act');
                $data = array();
                $data['uid'] = get_member_id();
                $data['dateline'] = time();
                if ($act == 'do') {
                        $data['isindex'] = '1';
                        $this->sysAdd("执行了【首推线路】操作，id=" . $id);
                } elseif ($act == 'un') {
                        $data['isindex'] = '0';
                        $this->sysAdd("执行了【取消首推线路】操作，id=" . $id);
                } else {
                        $this->iError("非法操作！");
                }
                $ret = M("Tours")->where(['id' => ['in', explode(',', $id)]])->data($data)->save();
                if ($ret !== false) {
                        $this->delSuccess('操作成功');
                } else {
                        $this->iError("数据库异常，请联系管理员解决。");
                }
        }

        /*
         * 显示
         */

        public function tours_show_show() {
                $id = I('get.id');
                $act = I('get.act');
                $data = array();
                $data['uid'] = get_member_id();
                $data['dateline'] = time();
                if ($act == 'do') {
                        $data['ischecked'] = '1';
                        $this->sysAdd("执行了【显示线路】操作，id=" . $id);
                } elseif ($act == 'un') {
                        $data['ischecked'] = '0';
                        $this->sysAdd("执行了【取消显示线路】操作，id=" . $id);
                } else {
                        $this->iError("非法操作！");
                }
                $ret = M("Tours")->where(['id' => ['in', explode(',', $id)]])->data($data)->save();
                if ($ret !== false) {
                        $this->delSuccess('操作成功');
                } else {
                        $this->iError("数据库异常，请联系管理员解决。");
                }
        }

        /*
         * 升级方案
         */

        public function add_case() {
                return $this->fetch();
        }

        /*
         * 行程方案
         */

        public function add_trip() {
                return $this->fetch();
        }

        /*
         * 产品管理
         */

        public function tours() {
                $map = $this->_search("Tours");
                if (I('post.title')) {
                        $map['title'] = array('like', "%" . I('post.title') . "%");
                }
                if (I('post.theme_id')) {
                        $map['theme_id'] = array('like', "%" . I('post.theme_id') . "%");
                }
                if (I('post.locationp_id')) {
                        $map['cid'] = array('like', "%" . I('post.locationp_id') . "%");
                }
                $this->_list(D("Tours"), $map, 'dateline', false, true);
                $theme = M('ToursTheme')->field('id,title')->select();
                $this->assign('theme', $theme);
                return $this->fetch();
        }

        /*
         * 产品管理添加
         */

        public function tours_add($id = '') {
                if (IS_POST) {
                        if (I('post.labels/a')) {
                                $_POST['labels'] = ',' . trim(implode(',', I('post.labels/a')), ',') . ',';
                        }
                        if (I('post.traffic/a')) {
                                $_POST['traffic'] = implode(',', I('post.traffic/a'));
                        }
                        if (I('post.theme_id/a')) {
                                $_POST['theme_id'] = ',' . trim(implode(',', I('post.theme_id/a')), ',') . ',';
                        }
                        $_POST['supplier_id'] = I('post.sup_id');
                        if (!I('post.locationt_id')) {
                                $this->iError('线路目的地请按层级选择！请勿随意填写');
                        }
                        $_POST['cid'] = ',' . trim(I('post.locationt_id'), ',') . ',';
                        $_POST['travelcompany'] = I('post.sup_name');
                        if (!empty($_POST['trip'])) {
                                $_POST['trip_days'] = count($_POST['trip']);
                        } else {
                                $_POST['trip_days'] = '0';
                        }
                        //图片
                        if (I('post.images/a')) {
                                $_POST['attachment'] = implode(',', I('post.images/a'));
                                $_POST['attachthumb'] = str_replace('_b', '_s', $_POST['attachment']);
                                if (I('post.img/d')) {
                                        if (!empty(I('post.images/a')[I('post.img/d') - 1])) {
                                                $_POST['attachpath'] = I('post.images/a')[I('post.img/d') - 1];
                                        } else {
                                                $_POST['attachpath'] = '';
                                        }
                                }
                        } else {
                                $_POST['attachment'] = '';
                                $_POST['attachthumb'] = '';
                        }

                        $model = M('Tours');
                        if ($id) {
                                $result = $model->auto('tours.edit')->create();
                                if (!$result) {
                                        return $this->iError($model->getError());
                                } else {
                                        $idk = $id;
                                        $list = $model->where(array('id' => $idk))->save();
                                        $list = $idk;
                                        $this->sysAdd("执行了【线路管理】编辑操作，id=" . $idk);
                                }
                        } else {
                                $result = $model->auto('tours.add')->create();
                                if (!$result) {
                                        return $this->iError($model->getError());
                                } else {
                                        $list = $model->add();
                                        $this->sysAdd("执行了【线路管理】添加操作，id=" . $list);
                                }
                        }
                        //价格上传
                        $min_price = array();
                        M("ToursPrice")->where(array('tours_id' => $list, 'case_id' => 0))->delete();
                        if (!empty($_POST['price'])) {
                                foreach ($_POST['price'] as $value) {
                                        $data = array();
                                        $data['tours_id'] = $list;
                                        $data['price_type'] = $value['type'];
                                        $data['leixin'] = $value['leixin'];
                                        $data['youhui'] = $value['youhui'];
                                        $price_value = array();
                                        switch ($value['type']) {
                                                case 1:
                                                        $price_value['adult'] = $value['adult'];
                                                        $data['sprice'] = $value['adult'];
                                                        break;
                                                case 2;
                                                        $price_value['adult'] = $value['adult'];
                                                        $data['sprice'] = $value['adult'];
                                                        break;
                                                case 3:
                                                        $price_value['adult'] = $value['adult'];
                                                        $data['sprice'] = $value['adult'];
                                                        break;
                                                case 4:
                                                        $price_value['adult'] = $value['adult'];
                                                        $data['sprice'] = $value['adult'];
                                                        break;
                                        }
                                        $data['price_value'] = serialize($price_value);
                                        if (!empty($data['sprice'])) {
                                                if ($data['sprice'] > 0) {
                                                        M("ToursPrice")->add($data);
                                                        $min_price[] = $data['sprice'];
                                                }
                                        }
                                }
                                $min_price = array_filter($min_price);
                                sort($min_price, SORT_NUMERIC);
                                $min_price = $min_price[0];
                                $model->where(array('id' => $list))->setField("price", $min_price);
                        }
                        //行程

                        M("ToursTrip")->where(array('tours_id' => $id))->delete();
                        $i = 0;
                        if (!empty($_POST['trip'])) {
                                foreach ($_POST['trip'] as $value) {
                                        $i++;
                                        $data = array();
                                        $data['tours_id'] = $id;
                                        $data['day'] = $i;
                                        if (!empty($value['title'])) {
                                                $data['title'] = $value['title'];
                                        }
                                        if (!empty($value['content'])) {
                                                $data['content'] = $value['content'];
                                        }
                                        if (!empty($value['breakfast'])) {
                                                $data['breakfast'] = $value['breakfast'];
                                        }
                                        if (!empty($value['lunch'])) {
                                                $data['lunch'] = $value['lunch'];
                                        }
                                        if (!empty($value['dinner'])) {
                                                $data['dinner'] = $value['dinner'];
                                        }
                                        if (!empty($value['lodging'])) {
                                                $data['lodging'] = $value['lodging'];
                                        }
                                        if (!empty($value['images'])) {
                                                $images = array();
                                                foreach ($value['images'] as $key => $v) {
                                                        $matches = array();
                                                        preg_match("/(.*\/)(.*)$/", $v, $matches);
                                                        $savepath = $matches[1];
                                                        $savename = $matches[2];
                                                        $images[] = array('savepath' => $savepath, 'savename' => $savename);
                                                }

                                                $data['images'] = serialize($images);
                                        }
                                        M("ToursTrip")->add($data);
                                }
                        }
                        //升级方案
                        M("ToursCase")->where(array('tours_id' => $id))->delete();
                        M("ToursPrice")->where(array('tours_id' => $id, 'case_id' => array('gt', 0)))->delete();
                        if (!empty($_POST['case'])) {
                                foreach ($_POST['case'] as $value) {
                                        $data = array();
                                        $data['id'] = $value['id'];
                                        $data['tours_id'] = $id;
                                        if (!empty($value['title'])) {
                                                $data['title'] = $value['title'];
                                        }
                                        if (!empty($value['content'])) {
                                                $data['content'] = $value['content'];
                                        }
                                        $case_id = M("ToursCase")->add($data);
                                        if ($case_id) {
                                                $case_min_price = array();
                                                if (!empty($value['price'])) {
                                                        foreach ($value['price'] as $v) {
                                                                $v = json_decode($v, true);
                                                                $data = array();
                                                                $sprice = array();
                                                                $data['tours_id'] = $id;
                                                                $data['start_time'] = $v['start_time'];
                                                                if (empty($v['start_time'])) {
                                                                        continue;
                                                                }
                                                                $data['end_time'] = $v['end_time'];
                                                                $data['price_type'] = $v['type'];
                                                                $data['case_id'] = $case_id;
                                                                $price_value = array();
                                                                switch ($v['type']) {
                                                                        case 1:
                                                                                $price_value['adult'] = $v['adult'];
                                                                                $price_value['tonghang'] = $v['tonghang'];
                                                                                $price_value['child'] = $v['child'];
                                                                                $data['sprice'] = $v['adult'];
                                                                                break;
                                                                        case 2;
                                                                                $sprice[] = array();
                                                                                $sprice[] = $price_value['adult_monday'] = $v['adult_monday'];
                                                                                $sprice[] = $price_value['adult_tuesday'] = $v['adult_tuesday'];
                                                                                $sprice[] = $price_value['adult_wednesday'] = $v['adult_wednesday'];
                                                                                $sprice[] = $price_value['adult_thursday'] = $v['adult_thursday'];
                                                                                $sprice[] = $price_value['adult_friday'] = $v['adult_friday'];
                                                                                $sprice[] = $price_value['adult_saturday'] = $v['adult_saturday'];
                                                                                $sprice[] = $price_value['adult_sunday'] = $v['adult_sunday'];
                                                                                $price_value['tonghang_monday'] = $v['tonghang_monday'];
                                                                                $price_value['tonghang_tuesday'] = $v['tonghang_tuesday'];
                                                                                $price_value['tonghang_wednesday'] = $v['tonghang_wednesday'];
                                                                                $price_value['tonghang_thursday'] = $v['tonghang_thursday'];
                                                                                $price_value['tonghang_friday'] = $v['tonghang_friday'];
                                                                                $price_value['tonghang_saturday'] = $v['tonghang_saturday'];
                                                                                $price_value['tonghang_sunday'] = $v['tonghang_sunday'];
                                                                                $price_value['child_monday'] = $v['child_monday'];
                                                                                $price_value['child_tuesday'] = $v['child_tuesday'];
                                                                                $price_value['child_wednesday'] = $v['child_wednesday'];
                                                                                $price_value['child_thursday'] = $v['child_thursday'];
                                                                                $price_value['child_friday'] = $v['child_friday'];
                                                                                $price_value['child_saturday'] = $v['child_saturday'];
                                                                                $price_value['child_sunday'] = $v['child_sunday'];
                                                                                $sprice = array_filter($sprice);
                                                                                sort($sprice, SORT_NUMERIC);
                                                                                $data['sprice'] = $sprice[0];
                                                                                break;
                                                                        case 3:
                                                                                $price_value['day'] = $v['day'];
                                                                                $sprice = $price_value['adult'] = $v['adult'];
                                                                                $price_value['tonghang'] = $v['tonghang'];
                                                                                $price_value['child'] = $v['child'];
                                                                                $sprice = array_filter($sprice);
                                                                                sort($sprice, SORT_NUMERIC);
                                                                                $data['sprice'] = $sprice[0];
                                                                                break;
                                                                        case 4:
                                                                                $price_value['adult'] = $v['adult'];
                                                                                $price_value['tonghang'] = $v['tonghang'];
                                                                                $price_value['child'] = $v['child'];
                                                                                $data['sprice'] = $v['adult'];
                                                                                break;
                                                                }
                                                                $data['price_value'] = serialize($price_value);
                                                                if ($data['sprice'] > 0) {
                                                                        M("ToursPrice")->add($data);
                                                                        $case_min_price[] = $data['sprice'];
                                                                }
                                                        }
                                                        $case_min_price = array_filter($case_min_price);
                                                        sort($case_min_price, SORT_NUMERIC);
                                                        $case_min_price = $case_min_price[0];
                                                        M("ToursCase")->where(array('id' => $case_id))->setField("price", $case_min_price);
                                                }
                                        }
                                }
                        }
                        return $this->iSucess('新增成功');
                } else {
                        if ($id) {
                                $tours = D("Tours")->field(true)->relation(true)->find($id);
                                if (!empty($tours['attachment'])) {
                                        $images = explode(',', $tours['attachment']);
                                } else {
                                        $images = array();
                                }
                                if (!empty($tours['tag'])) {
                                        $tours['tag'] = $tours['tag'];
                                } else {
                                        $tours['tag'] = '';
                                }

                                $this->assign('images', $images);
                                $this->assign('tours', $tours);
                        } else {
                                $tours = array();
                                $tours['cid'] = '';
                                $tours['type'] = '';
                                $tours['tag'] = '';
                                $tours['theme_id'] = '';
                                $tours['traffic'] = '';
                                $images = array();
                                $this->assign('images', $images);
                                $this->assign('tours', $tours);
                        }
                        //供应商
                        $supplier = M("Supplier")->cache()->where(array('type_id' => 1))->field('id,name')->select();
                        $this->assign('supplier', $supplier);

                        //类型
                        $toursType = M("ToursType")->cache()->field(true)->select();
                        $this->assign('toursType', $toursType);
                        //热销标签
                        $toursTag = M("ToursTag")->cache()->field(true)->select();
                        $this->assign('toursTag', $toursTag);
                        //主题
                        $theme = M('ToursTheme')->cache()->field('id,title')->order("id desc")->where(array('status' => 1))->select();
                        $this->assign('theme', $theme);

                        return $this->fetch();
                }
        }

        public function price_add() {
                if (IS_POST) {
                        $id = $_POST['id'];
                        //价格
                        M("ToursPrice")->where(array('tours_id' => $id, 'case_id' => 0))->delete();
                        if (!empty($_POST['price'])) {
                                foreach ($_POST['price'] as $value) {
                                        $data = array();
                                        $data['tours_id'] = $id;
                                        $sprice = array();
                                        $data['start_time'] = $value['start_time'];
                                        if (empty($value['start_time'])) {
                                                continue;
                                        }
                                        if (!empty($value['end_time'])) {
                                                $data['end_time'] = $value['end_time'];
                                        }
                                        $data['price_type'] = $value['type'];
                                        $data['leixin'] = $value['leixin'];
                                        $data['youhui'] = $value['youhui'];
                                        $price_value = array();
                                        switch ($value['type']) {
                                                case 1:
                                                        $price_value['adult'] = $value['adult'];
                                                        $price_value['tonghang'] = $value['tonghang'];
                                                        $price_value['child'] = $value['child'];
                                                        $data['sprice'] = $value['adult'];
                                                        break;
                                                case 2;
                                                        $sprice[] = array();
                                                        $sprice[] = $price_value['adult_monday'] = $value['adult_monday'];
                                                        $sprice[] = $price_value['adult_tuesday'] = $value['adult_tuesday'];
                                                        $sprice[] = $price_value['adult_wednesday'] = $value['adult_wednesday'];
                                                        $sprice[] = $price_value['adult_thursday'] = $value['adult_thursday'];
                                                        $sprice[] = $price_value['adult_friday'] = $value['adult_friday'];
                                                        $sprice[] = $price_value['adult_saturday'] = $value['adult_saturday'];
                                                        $sprice[] = $price_value['adult_sunday'] = $value['adult_sunday'];
                                                        $price_value['tonghang_monday'] = $value['tonghang_monday'];
                                                        $price_value['tonghang_tuesday'] = $value['tonghang_tuesday'];
                                                        $price_value['tonghang_wednesday'] = $value['tonghang_wednesday'];
                                                        $price_value['tonghang_thursday'] = $value['tonghang_thursday'];
                                                        $price_value['tonghang_friday'] = $value['tonghang_friday'];
                                                        $price_value['tonghang_saturday'] = $value['tonghang_saturday'];
                                                        $price_value['tonghang_sunday'] = $value['tonghang_sunday'];
                                                        $price_value['child_monday'] = $value['child_monday'];
                                                        $price_value['child_tuesday'] = $value['child_tuesday'];
                                                        $price_value['child_wednesday'] = $value['child_wednesday'];
                                                        $price_value['child_thursday'] = $value['child_thursday'];
                                                        $price_value['child_friday'] = $value['child_friday'];
                                                        $price_value['child_saturday'] = $value['child_saturday'];
                                                        $price_value['child_sunday'] = $value['child_sunday'];
                                                        $sprice = array_filter($sprice);
                                                        sort($sprice, SORT_NUMERIC);
                                                        $data['sprice'] = $sprice[0];
                                                        break;
                                                case 3:
                                                        $price_value['day'] = $value['day'];
                                                        $sprice = $price_value['adult'] = $value['adult'];
                                                        $price_value['tonghang'] = $value['tonghang'];
                                                        $price_value['child'] = $value['child'];
                                                        $sprice = array_filter($sprice);
                                                        sort($sprice, SORT_NUMERIC);
                                                        $data['sprice'] = $sprice[0];
                                                        break;
                                                case 4:
                                                        $price_value['adult'] = $value['adult'];
                                                        $price_value['tonghang'] = $value['tonghang'];
                                                        $price_value['child'] = $value['child'];
                                                        $data['sprice'] = $value['adult'];
                                                        break;
                                        }
                                        $data['price_value'] = serialize($price_value);
                                        if ($data['sprice'] > 0) {
                                                M("ToursPrice")->add($data);
                                                $min_price[] = $data['sprice'];
                                        }
                                }
                                $min_price = array_filter($min_price);
                                sort($min_price, SORT_NUMERIC);
                                $min_price = $min_price[0];
                                M("Tours")->auto('tours.edit')->create();
                                M("Tours")->where(array('id' => $id))->setField("price", $min_price);
                        }
                        $this->sysAdd("执行了【线路管理】添加价格操作，id=" . $id);
                        return $this->iSucess('新增成功');
                } else {
                        $id = $_GET['id'];
                        $price = D("ToursPrice")->where(array('tours_id' => $id, 'case_id' => 0))->order('id asc')->select();
                        foreach ($price as $key => $value) {
                                $price[$key]['price_value'] = unserialize($value['price_value']);
                        }
                        $this->assign('price', $price);
                        return $this->fetch();
                }
        }

        /*
         * 产品管理删除
         */

        public function tours_del() {
                $id = $_REQUEST['id'];
                //删除主表
                $condition = array('id' => array('in', explode(',', $id)));
                M("Tours")->where($condition)->delete();
                //删除行程
                $condition = array('tours_id' => array('in', explode(',', $id)));
                M("ToursTrip")->where($condition)->delete();
                //删除价格
                M("ToursPrice")->where($condition)->delete();
                //删除升级方案
                M("ToursCase")->where($condition)->delete();
                $this->sysAdd("执行了【删除线路】操作，id=" . $id);
                $this->delSuccess("删除成功！");
        }

        /*
         * 供应商管理
         */

        public function supplier() {
                $map = $this->_search("Supplier");
                $this->_list(D("Supplier"), $map, 'id', false, true);
                return $this->fetch();
        }

        /*
         * 供应商管理添加
         */

        public function supplier_add($id = '') {
                if (IS_POST) {
                        if ($id) {
                                $this->_update("Supplier", false, true);
                        } else {
                                $this->_insert("Supplier", false, true);
                        }
                } else {
                        if ($id) {
                                $nc = M("Supplier")->field(true)->find($id);
                                $this->assign('supplier', $nc);
                        } else {
                                $nc = array();
                                $nc['type_id'] = 1;
                                $this->assign('supplier', $nc);
                        }
                        return $this->fetch();
                }
        }

        /*
         * 供应商管理删除
         */

        public function supplier_del($id = '') {
                $this->_delete("Supplier", $id);
        }

        /*
         * 类型管理
         */

        public function line_type() {
                $map = $this->_search("ToursType");
                $this->_list(D("ToursType"), $map, 'id');
                return $this->fetch();
        }

        /*
         * 类型管理删除
         */

        public function line_type_del($id = '') {
                $this->_delete("ToursType", $id);
        }

        /*
         * 类型管理添加
         */

        public function line_type_add($id = '') {
                if (IS_POST) {
                        if ($id) {
                                $this->_update("ToursType", true, false);
                        } else {
                                $this->_insert("ToursType", true, false);
                        }
                } else {
                        if ($id) {
                                $nc = M("ToursType")->field(true)->find($id);
                                $this->assign('line_type', $nc);
                        } else {
                                $nc = array();
                                //$nc['type_id'] = 1;
                                $this->assign('line_type', $nc);
                        }
                        return $this->fetch();
                }
        }

        /*
         * 热门标签管理
         */

        public function tag() {
                $map = $this->_search("ToursTag");
                $this->_list(D("ToursTag"), $map, 'id');
                return $this->fetch();
        }

        /*
         * 热门标签管理删除
         */

        public function tag_del($id = '') {
                $this->_delete("ToursTag", $id);
        }

        /*
         * 热门标签管理添加
         */

        public function tag_add($id = '') {
                if (IS_POST) {
                        if ($id) {
                                $this->_update("ToursTag", true, false);
                        } else {
                                $this->_insert("ToursTag", true, false);
                        }
                } else {
                        if ($id) {
                                $nc = M("ToursTag")->field(true)->find($id);
                                $this->assign('tag', $nc);
                        } else {
                                $nc = array();
                                //$nc['type_id'] = 1;
                                $this->assign('tag', $nc);
                        }
                        return $this->fetch();
                }
        }

        /*
         * 主题管理
         */

        public function theme() {
                $map = $this->_search("ToursTheme");
                $this->_list(D("ToursTheme"), $map, 'id');
                return $this->fetch();
        }

        /*
         * 主题管理删除
         */

        public function theme_del($id = '') {
                $this->_delete("ToursTheme", $id);
        }

        /*
         * 主题管理添加
         */

        public function theme_add($id = '') {
                if (IS_POST) {
                        if ($id) {
                                $this->_update("ToursTheme", true, false);
                        } else {
                                $this->_insert("ToursTheme", true, false);
                        }
                } else {
                        if ($id) {
                                $nc = M("ToursTheme")->field(true)->find($id);
                                $this->assign('theme', $nc);
                        } else {
                                $nc = array();
                                $nc['status'] = 1;
                                $this->assign('theme', $nc);
                        }
                        return $this->fetch();
                }
        }

        /*
         * 标签管理
         */

        public function tours_play() {
                $map = $this->_search("ToursLabel");
                if (I('post.name')) {
                        $map['name'] = array('like', "%," . I('post.name') . ",%");
                }
                $this->_list(D("ToursLabel"), $map, 'id');
                return $this->fetch();
        }

        /*
         * 标签管理添加
         */

        public function tours_play_add($id = '') {
                if (IS_POST) {
                        $_POST['name'] = $_REQUEST['pplay_name'];
                        $_POST['prov_id'] = ',' . trim($_REQUEST['pplay_id'], ',') . ',';
                        if ($id) {
                                $this->_update("ToursLabel", false, false);
                        } else {
                                $this->_insert("ToursLabel", false, false);
                        }
                } else {
                        if ($id) {
                                $nc = M("ToursLabel")->field(true)->find($id);
                                $this->assign('tours_play', $nc);
                        } else {
                                $nc = array();
                                $nc['prov_id'] = '';
                                $nc['zhuozhong'] = 0;
                                $this->assign('tours_play', $nc);
                        }
                        return $this->fetch();
                }
        }

        /*
         * 标签管理删除
         */

        public function tours_play_del($id = '') {
                $this->_delete("ToursLabel", $id);
        }

        /*
         * 目的地维护
         */

        public function location_detail() {
                $map = $this->_search("LocationDetail");
                $this->_list(D("LocationDetail"), $map, 'id', false, true);
                return $this->fetch();
        }

        /*
         * 目的地维护添加
         */

        public function location_detail_add($id = '') {
                if (IS_POST) {
                        if ($id) {
                                $this->_update("LocationDetail", true, false);
                        } else {
                                $this->_insert("LocationDetail", true, false);
                        }
                } else {
                        if ($id) {
                                $nc = D("LocationDetail")->relation(true)->field(true)->find($id);
                                $this->assign('location_detail', $nc);
                        } else {
                                $nc = array();
                                $nc['status'] = 1;
                                $this->assign('location_detail', $nc);
                        }
                        return $this->fetch();
                }
        }

        /*
         * 目的地管理删除
         */

        public function location_detail_del($id = '') {
                $this->_delete("LocationDetail", $id);
        }

        /*
         * 目的地管理快速保存
         */

        public function location_detail_fast($id = '') {
                if (IS_POST) {
                        $model = M("LocationDetail");
                        $result = $model->create();
                        if ($id) {
                                $validate = \think\Loader::validate('LocationDetail');
                                if (!$validate->scene('add')->check($_REQUEST)) {
                                        return $this->iError($validate->getError());
                                }
                                if (!$result) {
                                        return $this->iError($model->getError());
                                } else {
                                        $pk = $model->getPk();
                                        $id = $_REQUEST[$pk];
                                        $list = $model->where(array($pk => $id))->save();
                                }
                                if ($list !== false) {
                                        $this->sysAdd("执行了【目的地维护】快速保存操作，id=" . $id);
                                        $data['forward'] = U('Product/location_detail_fast?id=' . $id);
                                        $data['statusCode'] = '200';
                                        $data['message'] = '快存成功';
                                        return $this->ajaxReturn($data);
                                } else {
                                        //失败提示
                                        return $this->iError('快存失败!');
                                }
                        } else {
                                $validate = \think\Loader::validate('LocationDetail');
                                if (!$validate->scene('edit')->check($_REQUEST)) {
                                        return $this->iError($validate->getError());
                                }
                                if (!$result) {
                                        return $this->iError($model->getError());
                                } else {
                                        $list = $model->add();
                                }
                                if ($list !== false) {
                                        $this->sysAdd("执行了【目的地维护】快速保存操作，id=" . $list);
                                        $data['forward'] = U('Product/location_detail_fast?id=' . $list);
                                        $data['statusCode'] = '200';
                                        $data['message'] = '快存成功';
                                        return $this->ajaxReturn($data);
                                } else {
                                        //失败提示
                                        return $this->iError('快存失败!');
                                }
                        }
                } else {
                        if ($id) {
                                $nc = D("LocationDetail")->relation(true)->field(true)->find($id);
                                $this->assign('location_detail', $nc);
                        } else {
                                $nc = array();
                                $nc['status'] = 1;
                                $this->assign('location_detail', $nc);
                        }
                        return $this->fetch('location_detail_add');
                }
        }

        /*
         * 热门景点
         */

        public function jingdian() {
                $map = $this->_search("Jingdian");
                if (I('post.locationk_id')) {
                        $map['jingdian_id'] = I('post.locationk_id');
                }
                $this->_list(D("Jingdian"), $map, 'id', false, true);
                return $this->fetch();
        }

        /*
         * 热门景点添加
         */

        public function jingdian_add($id = '') {
                if (IS_POST) {
                        if ($id) {
                                $this->_update("Jingdian", true, false);
                        } else {
                                $this->_insert("Jingdian", true, false);
                        }
                } else {
                        if ($id) {
                                $nc = D("Jingdian")->relation(true)->field(true)->find($id);
                                $this->assign('jingdian', $nc);
                        } else {
                                $nc = array();
                                $this->assign('jingdian', $nc);
                        }
                        return $this->fetch();
                }
        }

        /*
         * 热门景点删除
         */

        public function jingdian_del($id = '') {
                $this->_delete("Jingdian", $id);
        }

        /*
         * 置顶/不置顶
         */

        public function jingdian_show_istop() {
                $id = I('get.id');
                $act = I('get.act');
                $data = array();
                if ($act == 'do') {
                        $data['istop'] = '1';
                        $this->sysAdd("执行了【置顶热门景点】操作，id=" . $id);
                } elseif ($act == 'un') {
                        $data['istop'] = '0';
                        $this->sysAdd("执行了【取消置顶热门景点】操作，id=" . $id);
                } else {
                        $this->iError("非法操作！");
                }
                $ret = M("Jingdian")->where(['id' => ['in', explode(',', $id)]])->data($data)->save();
                if ($ret !== false) {
                        $this->delSuccess('操作成功');
                } else {
                        $this->iError("数据库异常，请联系管理员解决。");
                }
        }

        /*
         * 审核/不审核
         */

        public function jingdian_show_ischecked() {
                $id = I('get.id');
                $act = I('get.act');
                $data = array();
                if ($act == 'do') {
                        $data['status'] = '1';
                        $this->sysAdd("执行了【审核热门景点】操作，id=" . $id);
                } elseif ($act == 'un') {
                        $data['status'] = '0';
                        $this->sysAdd("执行了【取消审核热门景点】操作，id=" . $id);
                } else {
                        $this->iError("非法操作！");
                }
                $ret = M("Jingdian")->where(['id' => ['in', explode(',', $id)]])->data($data)->save();
                if ($ret !== false) {
                        $this->delSuccess('操作成功');
                } else {
                        $this->iError("数据库异常，请联系管理员解决。");
                }
        }

        /*
         * 景点快速保存
         */

        public function jingdian_add_fast($id = '') {
                if (IS_POST) {
                        $model = M("Jingdian");
                        $result = $model->create();
                        if ($id) {
                                $validate = \think\Loader::validate('Jingdian');
                                if (!$validate->scene('add')->check($_REQUEST)) {
                                        return $this->iError($validate->getError());
                                }
                                if (!$result) {
                                        return $this->iError($model->getError());
                                } else {
                                        $pk = $model->getPk();
                                        $id = $_REQUEST[$pk];
                                        $list = $model->where(array($pk => $id))->save();
                                }
                                if ($list !== false) {
                                        $this->sysAdd("执行了【热门景点】快速保存操作，id=" . $id);
                                        $data['forward'] = U('Product/jingdian_add_fast?id=' . $id);
                                        $data['statusCode'] = '200';
                                        $data['message'] = '快存成功';
                                        return $this->ajaxReturn($data);
                                } else {
                                        //失败提示
                                        return $this->iError('快存失败!');
                                }
                        } else {
                                $validate = \think\Loader::validate('Jingdian');
                                if (!$validate->scene('edit')->check($_REQUEST)) {
                                        return $this->iError($validate->getError());
                                }
                                if (!$result) {
                                        return $this->iError($model->getError());
                                } else {
                                        $list = $model->add();
                                }
                                if ($list !== false) {
                                        $this->sysAdd("执行了【热门景点】快速保存操作，id=" . $list);
                                        $data['forward'] = U('Product/jingdian_add_fast?id=' . $list);
                                        $data['statusCode'] = '200';
                                        $data['message'] = '快存成功';
                                        return $this->ajaxReturn($data);
                                } else {
                                        //失败提示
                                        return $this->iError('快存失败!');
                                }
                        }
                } else {
                        if ($id) {
                                $nc = D("Jingdian")->relation(true)->field(true)->find($id);
                                $this->assign('jingdian', $nc);
                        } else {
                                $nc = array();
                                $nc['status'] = 1;
                                $this->assign('jingdian', $nc);
                        }
                        return $this->fetch('jingdian_add');
                }
        }

        public function find_tag($id = '') {
                switch ($id) {
                        case 1:
                                echo '[{ "label": "团队餐", "value": "团队餐" }, {  "label": "自助", "value": "自助" }]';
                                break;
                        case 2:
                                echo '[{"label": "火车上", "value": "火车上" }, { "label": "飞机上", "value": "飞机上" },{ "label": "舒适旅馆", "value": "舒适旅馆" },{ "label": "豪华酒店", "value": "豪华酒店" }]';
                                break;
                }
        }

}
